<?php


if (!eregi("admin.php", $_SERVER['PHP_SELF'])) { die ("Access Denied"); }
global $prefix, $db;
$aid = substr("$aid", 0,25);
$row = $db->sql_fetchrow($db->sql_query("SELECT radminsuper FROM " . $prefix . "_authors WHERE aid='$aid'"));
if ($row['radminsuper'] == 1) {

function anbmain(){
	global $prefix, $db, $bgcolor2;
	$result = $db->sql_query("SELECT backupdir, lastdb, lastfile, autolast, autoinc FROM ".$prefix."_advbackup where id='1'");
	$row = $db->sql_fetchrow($result);
	$backupdir = $row['backupdir'];
	$lastdb = $row['lastdb'];
	$lastfile = $row['lastfile'];
	$autolast = $row['autolast'];
	$autoinc = $row['autoinc'];

	$sum = $autoinc * 3600 + $autolast;
	$nextauto = date("m-d-y@h-ia", $sum);
	$lastauto = date("m-d-y@h-ia", $autolast);
	echo "<script language=\"javascript\">
		function toggle(targetId) {
			target = document.all(targetId);
			if (target.style.display == \"none\"){
				target.style.display=\"\";
			} else {
				target.style.display=\"none\";
			}
		}
		</script>";
	include("header.php");
	opentable2();
	echo "<center><font class=\"title\">Otomatik Yedek Alma Servisi</font></center>";
	closetable2();
	echo "<br>";
	opentable();
	echo "<center>[ <a href=\"admin.php?op=anbconfig\"><b>Oto Yedek Ayarları</b></a> ] [ <a href=\"admin.php\"><b>Yönetim Menüsü</b></a> ] </center>";
	closetable();
	echo "<br>";
	opentable();
	echo "<form action=\"admin.php\" method=\"post\">"
		."<table width=\"100%\" align=\"center\">"
		."<tr><td colspan=\"2\"><center><font class=\"title\">Hemen Şimdi Yedeği Al <br>SQL Yedek Bilgisi</font><br>En son yedek alım zamanı : [$lastdb]</center></td></tr>"
		."<tr bgcolor=\"$bgcolor2\"><td><b>Yedeği Sıkıştır :</b></td><td><input type=\"checkbox\" name=\"use_gzip\" checked></td></tr>"
		."<tr><td><b>E-Mail İle Gönder :</b></td><td><input type=\"checkbox\" name=\"maildb\"></td></tr>"
		."<tr bgcolor=\"$bgcolor2\"><td><b>E-Mail Gönderdikten Sonra Sil :</b></td><td><input type=\"checkbox\" name=\"deldbback\"></td></tr>"
		."<input type=\"hidden\" name=\"op\" value=\"backdb\">"
		."<input type=\"hidden\" name=\"dobackdb\" value=\"yes\">"
		."<tr><td colspan=\"2\"><br><input type=\"submit\" value=\"Hemen Şimdi Yedeği Al\"><br><br></td>"
		."</tr>"
		."</form></table>"
		."<a href=\"javascript:toggle('showbackups')\">Veritabanı Yedeklerini Göster/Gizle</a><br><br>"
		."<table id=\"showbackups\" style=\"display: none\">";

	$handle=opendir("./$backupdir");
	while ($file = readdir($handle)){
		if ($file != "." && $file != ".."){
			if (strrpos($file, ".")){
				$tlist .= "$file ";
			}
		}
	}
	closedir($handle);
	$tlist = explode(" ", $tlist);
	sort($tlist);
	for ($i = 0; $i < sizeof($tlist); $i++){
		if($tlist[$i] != "") {
			echo "<tr><td><a href=\"$backupdir/$tlist[$i]\">$tlist[$i]</a></td></tr>";
		}
	}

	echo "</table>";
	closetable();
	echo "<br>";
	opentable();
	echo "<form action=\"admin.php\" method=\"post\">"
		."<table width=\"100%\" align=\"center\">"
		."<tr><td colspan=\"2\"><center><font class=\"title\">Hemen Şimdi <br>Klasör Yada Dosya Yedeği Al</font><br>En son dosya yedek alım zamanı : [$lastfile]</center></td></tr>"
		."<tr bgcolor=\"$bgcolor2\"><td><b>E-Mail İle Gönder  :</b></td><td><input type=\"checkbox\" name=\"mailfiles\"></td></tr>"
		."<tr><td><b>E-Mail Gönderdikten Sonra Sil :</b></td><td><input type=\"checkbox\" name=\"remove_gzip_file\"></td></tr>"
		."<input type=\"hidden\" name=\"op\" value=\"backfiles\">"
		."<input type=\"hidden\" name=\"dobackfiles\" value=\"yes\">"
		."<tr><td colspan=\"2\"><br><input type=\"submit\" value=\"Hemen Şimdi Yedeği Al\"><br><br></td>"
		."</tr>"
		."</form></table>"
		."<a href=\"javascript:toggle('showfiles')\">Yedeklenen Dosyaları Göster/Gizle</a><br><br>"
		."<table id=\"showfiles\" style=\"display: none\">";

	$handle2=opendir("./$backupdir/file");
	while ($file2 = readdir($handle2)){
		if ($file2 != "." && $file2 != ".."){
			if (strrpos($file2, ".")){
				$tlist2 .= "$file2 ";
			}
		}
	}
	closedir($handle2);
	$tlist2 = explode(" ", $tlist2);
	sort($tlist2);
	for ($i = 0; $i < sizeof($tlist2); $i++){
		if($tlist2[$i] != "") {
			echo "<tr><td><a href=\"$backupdir/file/$tlist2[$i]\">$tlist2[$i]</a></td></tr>";
		}
	}

	echo "</table>";
		
	closetable();
	include("footer.php");
}

function backdb($dobackdb, $use_gzip, $maildb, $deldbback2){
	global $prefix, $db, $dbhost, $dbuname, $dbpass, $dbname;

	$result = $db->sql_query("SELECT * FROM ".$prefix."_advbackup");
	$row = $db->sql_fetchrow($result);
		$backupdir = $row['backupdir'];
		$mail_to = $row['mail_to'];
		$mail_from = $row['mail_from'];
	if ($dobackdb == "yes"){
		$manual = "yes";
		include("includes/dump.php");
	}
	

	include("header.php");
	opentable2();
	echo "<center><font class=\"title\">Sistem Yedek</font></center>";
	closetable2();
	echo "<br>";
	opentable();
	echo "<center>[ <a href=\"admin.php?op=anbmain\"><b>Otomatik Yedek Alma Servisi</b></a> ] [ <a href=\"admin.php\"><b>Yönetim Menüsü </b></a> ] </center>";
	closetable();
	echo "<br>";
	opentable();
	if(file_exists($filename2)){
		echo "<center>Veritabanınız Başarıyla Yedeklenmiştir!</center>";
	}else{
		echo "<center>Üzgünüz Ama 1 Hata Oluşumu Algılandı Yedek Alma Sırasında!</center>";
	}
	if($deldbback2){
		unlink($filename2);
	}
	closetable();
	include("footer.php");
}

function backfiles($dobackfiles, $mailfiles, $remove_gzip_file){
	global $prefix, $db;
	$result = $db->sql_query("SELECT * FROM ".$prefix."_advbackup where id='1'");
	$row = $db->sql_fetchrow($result);
	$backupdir = $row['backupdir'];
	$mail_to = $row['mail_to'];
	$mail_from = $row['mail_from'];
	$excludes = $row['excludes'];
	ignore_user_abort();

	if ($dobackfiles == "yes"){
		if (!is_dir("$backupdir/file")){
			mkdir("$backupdir/file", 0755);
		}
		$date2 = date("m-d-y");
		$dst = "".$date2."_files";
		$split = split("\n", $excludes);
		$join = join("--exclude=", $split);
		$join2 = str_replace(Chr(13), " ", $join);
		$exc = "--exclude=$join2";
		$zipline = "tar -czf $backupdir/file/$dst.tar.gz --exclude=$backupdir $exc * .[a-zA-Z]*";
			shell_exec($zipline);
		if (file_exists("$backupdir/file/$dst.tar.gz")){
			$fileres = "&middot; Your Files were backed up successfully!<br>";
			$datetime = date("m-d-y@h-ia");
			$result = $db->sql_query("UPDATE ".$prefix."_advbackup set lastfile='$datetime' where id='1'");
		}else{
			$fileres = "&middot; An error occured! The files could not be backed up<br>";
		}
	}

	if($mailfiles){
		$senddate = date("m-d-y@h-ia");
		$subject = "Files Backup - $senddate"; 

		$filename3 = "$backupdir/file/$dst.tar.gz";
		$fileatt_type = filetype($filename3);
		$fileatt_name = str_replace("/", "", strrchr($filename3, "/"));
		$file = fopen($filename3,'rb');
		$data = fread($file,filesize($filename3));
		fclose($file);
		$data = chunk_split(base64_encode($data));

		$semi_rand = md5(time());
		$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

		$headers = "From: $mail_from <$mail_from>\n";
		$headers .= "MIME-Version: 1.0\n";
		$headers .= "Content-Type: multipart/mixed;\n";
		$headers .= " boundary=\"{$mime_boundary}\"";

		$message = "This is a multi-part message in MIME format.\n\n";
		$message .= "--{$mime_boundary}\n";
		$message .= "Content-Type: text/plain; charset=\"UTF-8\"\n";
		$message .= "Content-Transfer-Encoding: 7bit\n\n";
		$message .= "Dosyalarınızın Yedeği Alındı ve E-maile Dosya Alarak Eklendi!\n";
		$message . "\n\n";
		$message .= "--{$mime_boundary}\n";
		$message .= "Content-Type: {$fileatt_type};\n";
		$message .= " name=\"{$fileatt_name}\"\n";
		$message .= "Content-Disposition: attachment;\n";
		$message .= " filename=\"{$fileatt_name}\"\n";
		$message .= "Content-Transfer-Encoding: base64\n\n";
		$message .= $data . "\n\n";
		$message .= "--{$mime_boundary}--\n";

		$check3 = @mail($mail_to, $subject, $message, $headers, "-f$mail_from");
		if ($check3){
			$mailfileres = "&middot; Files have been mailed!<br>";
		}else{
			$mailfileres = "&middot; Mail could not be sent. [ <a href=\"javascript:history.go(-1)\">Go Back</a> ]<br>";
		}
		if($remove_gzip_file){
			unlink("$backupdir/file/$dst.tar.gz");
		}
	}

	include("header.php");
	opentable2();
	echo "<center><font class=\"title\">Otomatik Yedek Alma Servisi</font></center>";
	closetable2();
	echo "<br>";
	opentable();
	echo "<center>[ <a href=\"admin.php?op=anbmain\"><b>Otomatik Yedek Alma Servisi Anasayfa</b></a> ] [ <a href=\"admin.php\"><b>Yönetim Menüsü</b></a> ] </center>";
	closetable();
	echo "<br>";
	opentable();
	echo "$fileres $mailfileres";
	closetable();
	include("footer.php");
}

function anbconfig(){
	global $bgcolor2, $prefix, $db;
	include("header.php");
	echo "<script language=\"javascript\">
		function toggle(targetId) {
			target = document.all(targetId);
			if (target.style.display == \"none\"){
				target.style.display=\"\";
			} else {
				target.style.display=\"none\";
			}
		}
		</script>";
	$result = $db->sql_query("SELECT * FROM ".$prefix."_advbackup");
	$row = $db->sql_fetchrow($result);
		$backupdir = $row['backupdir'];
		$mail_to = $row['mail_to'];
		$mail_from = $row['mail_from'];
		$excludes = $row['excludes'];
		$autostate = $row['autostate'];
		$autoinc = $row['autoinc'];
		$autogzip = $row['autogzip'];
		$automail = $row['automail'];
		$deldbback = $row['autodel'];

	if ($autostate == "on"){$statecheck = "checked";}
	if ($autogzip == "on"){$gzipcheck = "checked";}
	if ($automail == "on"){$mailcheck = "checked";}
	if ($deldbback == "on"){$deldbcheck = "checked";}

	opentable2();
	echo "<center><font class=\"title\">Otomatik Yedek Alma Servisi Ayarları</font></center>";
	closetable2();
	echo "<br>";
	opentable();
	echo "<center>[ <a href=\"admin.php?op=anbmain\"><b>Otomatik Yedek Alma Servisi Anasayfası</b></a> ] [ <a href=\"admin.php\"><b>Yönetim Menüsü</b></a> ] </center>";
	closetable();
	echo "<br>";
	opentable();
	echo "<form action=\"admin.php\" method=\"post\">"
		."<table width=\"100%\" align=\"center\">"
		."<tr><td colspan=\"2\"><center><font class=\"title\">Genel Ayarlar</font></center></td></tr>"
		."<tr bgcolor=\"$bgcolor2\"><td width=\"65%\">Yedek alma dizini:</td><td align=\"right\"> <input type=\"text\" name=\"backupdir2\" value=\"$backupdir\" size=\"30\"></td></tr>"
		."<tr><td width=\"65%\">Gönderilecek mail adresi:</td><td align=\"right\"> <input type=\"text\" name=\"mail_to2\" value=\"$mail_to\" size=\"30\"></td></tr>"
		."<tr bgcolor=\"$bgcolor2\"><td width=\"65%\">Gönderen mail adresi:</td><td align=\"right\"> <input type=\"text\" name=\"mail_from2\" value=\"$mail_from\" size=\"30\"></td></tr>"
		."</table>";
	closetable();
	echo "<br>";
	opentable();
		echo "<table width=\"100%\" align=\"center\">"
		."<tr><td colspan=\"2\"><center><font class=\"title\">Yedek Ayarları</font></center></td></tr>"
		."<tr bgcolor=\"$bgcolor2\"><td width=\"65%\">Otomatik Yedek Alma Servisi Açık : </td><td align=\"right\"><input type=\"checkbox\" name=\"autostate2\" $statecheck></td></tr>"
		."<tr><td width=\"65%\">Ne Zamanda Bir Yedek Alınsın : (1-999 Arasında bir saat yazınız)</td><td align=\"right\"><input type=\"text\" name=\"autoinc2\" value=\"$autoinc\" size=\"4\" maxlength=\"3\"></td></tr>"
		."<tr bgcolor=\"$bgcolor2\"><td width=\"65%\">Yedek Sıkıştırma Açık : (gzip)</td><td align=\"right\"><input type=\"checkbox\" name=\"autogzip2\" $gzipcheck></td></tr>"
		."<tr><td width=\"65%\">Yedek E-Mail İle Gönderilsin mi? : </td><td align=\"right\"><input type=\"checkbox\" name=\"automail2\" $mailcheck></td></tr>"
		."<tr bgcolor=\"$bgcolor2\"><td width=\"65%\">E-Mail Gönderildikten Sonra Yedek Dizinden Silinsin mi? : </td><td align=\"right\"><input type=\"checkbox\" name=\"deldbback2\" $deldbcheck></td></tr>"
	    ."</table>";
	closetable();
	echo "<br>";
	opentable();
	echo "<table width=\"100%\" align=\"center\">"
		."<center><font class=\"title\">Dosyalarınızıda Yedeklemek İsterseniz</font></center>"
		."<tr bgcolor=\"$bgcolor2\"><td width=\"65%\" valign=\"top\">Exclude Folders: <br> - Yedek Alınan Klasör Otomatik Olarak İhraç Edilmiştir<br> - Her Satıra Bir Klasör İsmi Yazınız<br> - Dosya Adları Küçük Büyük Harf Duyarlıdır<br></td>"
		."<td align=\"center\" valign=\"top\"><textarea name=\"excludes2\" cols=\"30\" rows=\"8\" wrap=\"virtual\">$excludes</textarea><br><a href=\"javascript:toggle('showfolders')\">Klasörleri Göster/Gizle</a></td>"
		."<td id=\"showfolders\" style=\"display: none\">"
		."<table>";

	$handle=opendir("./");
	while ($file = readdir($handle)){
		if ($file != "." && $file != ".."){
			if (is_dir($file)){
				$tlist .= "$file ";
			}
		}
	}
	closedir($handle);
	$tlist = explode(" ", $tlist);
	sort($tlist);
	for ($i = 0; $i < sizeof($tlist); $i++){
		if($tlist[$i] != "") {
			echo "<tr><td>$tlist[$i]</td></tr>";
		}
	}

	echo "</table></td></tr>"
		."<tr><td colspan=\"2\"><input type=\"hidden\" name=\"op\" value=\"setconfig\">"
		."<br><br><center><input type=\"submit\" value=\"Save Changes\"></center></td>"
		."</tr></table></form>";
	closetable();
	include("footer.php");
}

function setconfig($backupdir2, $mail_to2, $mail_from2, $excludes2, $autostate2, $autoinc2, $autogzip2, $automail2, $deldbback2){
	global $prefix, $db;
	$mail_to2 = stripslashes($mail_to2);
	$mail_from2 = stripslashes($mail_from2);
	$result = $db->sql_query("UPDATE " . $prefix . "_advbackup SET backupdir='$backupdir2', mail_to='$mail_to2', mail_from='$mail_from2', excludes='$excludes2', autostate='$autostate2', autoinc='$autoinc2', autogzip='$autogzip2', automail='$automail2', autodel='$deldbback2' WHERE id='1'");
	if (!is_dir("$backupdir2")){
			mkdir("$backupdir2", 0755);
		}
	include("header.php");
	opentable2();
	echo "<center><font class=\"title\">Otomatik Yedek Alma Servisi</font></center>";
	closetable2();
	echo "<br>";
	opentable();
	echo "<center>[ <a href=\"admin.php?op=anbmain\"><b>Otomatik Yedek Alma Servisi Anasayfa</b></a> ] [ <a href=\"admin.php\"><b>Yönetici Menüsü</b></a> ] </center>";
	closetable();
	echo "<br>";
	if (!$result){
		opentable();
		echo "<center>An error occured! [ <a href=\"javascript:history.go(-1)\">Go Back</a> ]</center>";
		closetable();
	}else{
		opentable();
		echo "<center>Bilgiler Güncellendi!</center>";
		closetable();
	}
	include("footer.php");
}

switch ($op) {

	default:
	anbmain();
	break;

	case "backdb":
	backdb($dobackdb, $use_gzip, $maildb, $deldbback2);
	break;

	case "backfiles":
	backfiles($dobackfiles, $mailfiles, $remove_gzip_file);
	break;

	case "advbackup":
	advbackup($backdb, $use_gzip, $maildb, $backfiles, $mailfiles, $remove_gzip_file);
	break;

	case "anbconfig":
	anbconfig();
	break;

	case "setconfig":
	setconfig($backupdir2, $mail_to2, $mail_from2, $excludes2, $autostate2, $autoinc2, $autogzip2, $automail2, $deldbback2);
	break;

}
}
?>